package cn.topevery.garden.wapi.utils;

import java.util.regex.Pattern;

public class RegexUtils {
    /// <summary>
    /// 正整数匹配规则
    /// </summary>
    public static String Uint_Pattern = "[1-9]\\d*";

    /// <summary>
    /// 正浮点数匹配规则
    /// </summary>
    public static String UFloat_Pattern = "[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*";

    /// <summary>
    /// 是否强密码
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsStrongPassWord(String input)
    {
        return IsMatch("^((?=\\S*?[A-Z])(?=\\S*?[a-z])(?=\\S*?[0-9]).{7,})\\S$", input);
    }

    /// <summary>
    /// 是否存在连续重复的数字/字母/符号/中文
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsHaveRepeat(String input)
    {
        return IsMatch("(\\w|\\S)\\1{2}", input);
    }

    /// <summary>
    /// 是否存在连续数字或字母
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsHaveContinuous(String input)
    {
        return IsMatch("(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2}\\d|(a(?=b)|b(?=c)|c(?=d)|d(?=e)|e(?=f)|f(?=g)|g(?=h)|h(?=i)|i(?=j)|j(?=k)|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o(?=p)|p(?=q)|q(?=r)|r(?=s)|s(?=t)|t(?=u)|u(?=v)|v(?=w)|w(?=x)|x(?=y)|y(?=z)){2}[a-z]", input);
    }

    /// <summary>
    /// 判断输入的字符串只包含汉字
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsChineseCh(String input)
    {
        return IsMatch("^[\\u4e00-\\u9fa5]+$", input);
    }

    /// <summary>
    /// 判断是否包含中文
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static Boolean HasChinese(String input)
    {

        return IsMatch("[\\u4e00-\\u9fa5]", input);
    }

    /// <summary>
    /// 匹配3位或4位区号的电话号码，其中区号可以用小括号括起来，
    /// 也可以不用，区号与本地号间可以用连字号或空格间隔，
    /// 也可以没有间隔
    /// \\(0\\d{2}\\)[- ]?\\d{8}|0\\d{2}[- ]?\\d{8}|\\(0\\d{3}\\)[- ]?\\d{7}|0\\d{3}[- ]?\\d{7}
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsPhone(String input)
    {
        String pattern = "^\\\\(0\\\\d{2}\\\\)[- ]?\\\\d{8}$|^0\\\\d{2}[- ]?\\\\d{8}$|^\\\\(0\\\\d{3}\\\\)[- ]?\\\\d{7}$|^0\\\\d{3}[- ]?\\\\d{7}$";
        return IsMatch(pattern, input);
    }

    /// <summary>
    /// 判断输入的字符串是否是一个合法的手机号
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsMobilePhone(String input)
    {
        return IsMatch("^(13|14|15|17|18|19)[0-9]{9}$", input);
    }


    /// <summary>
    /// 判断输入的字符串只包含数字
    /// 可以匹配整数和浮点数
    /// ^-?\\d+$|^(-?\\d+)(\\.\\d+)?$
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsNumber(String input)
    {
        String pattern = "^-?\\\\d+$|^(-?\\\\d+)(\\\\.\\\\d+)?$";
        return IsMatch(pattern, input);
    }


    /// <summary>
    /// 匹配非负整数
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsNotNagtive(String input)
    {
        return IsMatch("^\\d+$", input);
    }



    /// <summary>
    /// 匹配正整数
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsUint(String input)
    {
        return IsMatch(Uint_Pattern, input);
    }

    /// <summary>
    /// 判断输入的字符串字包含英文字母
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsEnglisCh(String input)
    {
        return IsMatch("^[A-Za-z]+$", input);
    }


    /// <summary>
    /// 判断输入的字符串是否是一个合法的Email地址
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsEmail(String input)
    {
        String pattern = "^([\\w-\\.]+)((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        return IsMatch(pattern, input);
    }


    /// <summary>
    /// 判断输入的字符串是否只包含数字和英文字母
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsNumAndEnCh(String input)
    {
        return IsMatch("^[A-Za-z0-9]+$", input);
    }


    /// <summary>
    /// 判断输入的字符串是否是一个超链接
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Boolean IsURL(String input)
    {
        String pattern = "^[a-zA-Z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
        return IsMatch(pattern, input);
    }

    /// <summary>
    /// 调用Regex中IsMatch函数实现一般的正则表达式匹配
    /// </summary>
    /// <param name="pattern">要匹配的正则表达式模式。</param>
    /// <param name="input">要搜索匹配项的字符串</param>
    /// <returns>如果正则表达式找到匹配项，则为 true；否则，为 false。</returns>
    public static Boolean IsMatch(String pattern, String input)
    {
        if (input == null || input == "") {
            return false;
        }
        return Pattern.matches(pattern,input);
    }


}
